#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"

static void show_char(const void *data);
static void show_string(const void *data);
static int str_cmp(const void *a, const void *b);


int main(int argc, char *argv[])
{
    // 存argv[1]字符串的每一个字符
    seqlist_t *c_list = NULL;
    // 存argv每一个字符串, 顺序表中存储argv每一个参数字符串的起始地址
    seqlist_t *str_list = NULL;

    int i;
    char *str1 = "123";
    char **p = &str1, **res = NULL;

    if (argc < 2)
        return 1;

    seqlist_init(&c_list, strlen(argv[1]), sizeof(char));
    seqlist_init(&str_list, argc, sizeof(char *));

    for (i = 0; argv[1][i]; i++)	
        seqlist_insert(c_list, argv[1] + i);
    seqlist_traval(c_list, show_char);

    for (i = 0; i < argc; i++)
        seqlist_insert_sort(str_list, argv + i, str_cmp);
    seqlist_traval(str_list, show_string);

    res = (char **)seqlist_find(str_list, p, str_cmp);
    if (NULL != res)
        printf("查找：%s\n", *(res));

    printf("删除：%s\n", *p);
    seqlist_delete(str_list, p, str_cmp);

    seqlist_traval(str_list, show_string);


    seqlist_destroy(&c_list);
    seqlist_destroy(&str_list);
    // 不出错 str_list = NULL;
    seqlist_destroy(&str_list);

    return 0;
}

static void show_char(const void *data)
{
    char *d = (char *)data;
    printf("%c", *d);
}

static void show_string(const void *data)
{
    char **d = (char **)data;

    printf("%s ", *d);
}


static int str_cmp(const void *a, const void *b)
{
    char **t1 = (char **)a;
    char **t2 = (char **)b;

    return strcmp(*t1, *t2);
}

